// pages/my/PointsShop/SettlementConfirmation/SettlementConfirmation.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasDefAdress: false,//是否有默认地址
        isDefAdress: true,//是否是默认地址
        adress: {},//收获地址
        shopId: 0,//积分商品id
        shop: {},//积分商品信息
        adressId: 1,//收获地址id
        orderId: 1,//订单id
        
    },
    //支付订单
    pay() {
        wx.request({
            url: 'http://localhost:8080/keep/order/scoreOrder',
            method:'POST',
            header: {
                authorization: getApp().globalData.authorization
            },
            data: {
                userId: getApp().globalData.loginfo.userId,
                orderPrice: this.data.shop.goodsScore,
                orderStatus:1,
                addressId: this.data.adressId,
                shopId: this.data.shopId
            },
            success: res => {
                if (res.data.code == 200 && res.data.data!=-1) {
                    this.setData({
                        orderId:res.data.data
                    },()=>{
                        wx.navigateTo({
                            url: '/pages/my/PointsShop/paySuccess/paySuccess?score='+this.data.shop.goodsScore+'&id='+this.data.orderId,
                          })
                    })
                    
                }else{
                    wx.showToast({
                      title: '积分不足',
                    })
                }
            }
        });
    },
    //发送请求获取商品数据
    getShop() {
        wx.request({
            url: 'http://localhost:8080/score/goods/' + this.data.shopId,
            header: {
                authorization: getApp().globalData.authorization
            },
            success: res => {
                if (res.data.code == 200) {
                    var shop = res.data.data.goodsPicture;
                    shop = JSON.parse(shop);
                    res.data.data.goodsPicture = shop;
                    res.data.data.goodsPicture = res.data.data.goodsPicture[0].url;
                    this.setData({
                        shop: res.data.data,
                    })
                }
            }
        });
    },
    //选择收获地址
    changeAdress() {
        wx.request({
            url: 'http://localhost:8080/keep/address/' + this.data.adressId,
            header: {
                authorization: getApp().globalData.authorization
            },
            success: res => {
                if (res.data.code == 200 && res.data.data != null) {
                    if (res.data.data.fixed == 1) {
                        this.setData({
                            adress: res.data.data,
                            isDefAdress: false
                        })
                    } else if (res.data.data.fixed == 0) {
                        this.setData({
                            adress: res.data.data,
                            isDefAdress: true
                        })
                    }
                } else {
                    //提示更改失败
                }
            }
        });
    },
    //查询地址
    getDefAdress() {
        wx.request({
            url: 'http://localhost:8080/keep/address/userId/' + getApp().globalData.loginfo.userId,
            header: {
                authorization: getApp().globalData.authorization
            },
            success: res => {
                if (res.data.code == 200 && res.data.data != null) {
                    this.setData({
                        adressId: res.data.data.id,
                        hasDefAdress: true
                    }, () => {
                        this.changeAdress();
                    })
                } else {
                    this.setData({
                        hasDefAdress: false
                    }, () => {
                        this.changeAdress();
                    })
                }
            }
        });
    },
    // 返回上一页
    onNavigate() {
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            shopId: options.id
        }, () => {
            this.getDefAdress();
            this.getShop();
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.changeAdress();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})